<?php


namespace App\Models\Myquery;
use App\Http\Common\MyUtils;
use Illuminate\Database\Eloquent\Builder as BaseBuilder;
use \Illuminate\Database\Query\Builder as QueryBuilder;
use App\Models\Myquery\MyQueryBuilder as MyQueryBuilder;
use Illuminate\Support\Facades\Redis;

/**
 * 我的builder 继承了larvel自带的builder，$this->setTableRedisUpdated()是为了实现redis删除，修改，插入的时候自动更新redis
 * Class Builder
 * @package App\Models\Myquery
 */
class Builder extends BaseBuilder
{
    public $filter = ['content','brief','name','title','description'];// 不需要过滤关键字的字段
    public function __construct(QueryBuilder $query)
    {
        // 用自己的query实现，实现redis快速加载
        $query = new MyQueryBuilder($query->connection,$query->grammar,$query->processor);
        $this->query = $query;
    }
    public function delete()
    {
        $this->setTableRedisUpdated();
        return parent::delete();
    }
    public function update(array $values)
    {
        $this->setTableRedisUpdated();
        $this->filterScensitive($values);
        return parent::update($values);
    }
    public function insert(array $values)
    {
        $this->setTableRedisUpdated();
        $this->filterScensitive($values);
        return parent::insert($values);
    }
    public function insertGetId(array $values, $sequence = null)
    {
        $this->setTableRedisUpdated();
        $this->filterScensitive($values);
        return parent::insertGetId($values, $sequence);
    }
    public function updateOrInsert(array $attributes, array $values = [])
    {
        $this->setTableRedisUpdated();
        $this->filterScensitive($values);
        return parent::updateOrInsert($attributes, $values);
    }
    public function insertUsing(array $columns, $query)
    {
        $this->setTableRedisUpdated();
        return parent::insertUsing($columns, $query);
    }
    public function lockForUpdate()
    {
        $this->setTableRedisUpdated();
        return parent::lockForUpdate();
    }
    public function updateOrCreate(array $attributes, array $values = [])
    {
        $this->setTableRedisUpdated();
        $this->filterScensitive($values);
        return parent::updateOrCreate($attributes, $values);
    }
    protected function addUpdatedAtColumn(array $values)
    {
        $this->setTableRedisUpdated();
        $this->filterScensitive($values);
        return parent::addUpdatedAtColumn($values);
    }
    public function forceDelete()
    {
        $this->setTableRedisUpdated();
        return parent::forceDelete();
    }
    /**
     * 设置 该表 的 redis的 更新状态为更新
     * 做一系列redis更新 ，防止出现有的修改不会更新redis数据的情况
     * @param string $tableName 表名字
     */
    public function setTableRedisUpdated($tableName=''){
        if(env('REDIS_OPEN')){
            $this->updateRedis($this->model);
            foreach ($this->model->redisClass as $k=>$v){
                $this->updateRedis(new $v);
            }
        }
    }
    /**
     * 更新redis
     * @param $model
     */
    public function updateRedis($model){
        if ($model)$tableName = $model->getTable();
        // 修改之后，会自动更新redis 里面的 该 表名-table-updated = true
        // 查询所有的env('DB_PREFIX') 开头的 所有的表的名称
        Redis::set(env('DB_PREFIX').$tableName.'-table-update-time',time());
        foreach ($model->redisTable as $k=>$v){
            Redis::set(env('DB_PREFIX').$v.'-table-update-time',time());
        }
        if (isset($model->findAllList)){
            MyUtils::getTablesArray($model->findAllList,$allTableNames);
            foreach ($allTableNames as $k=>$v){
                Redis::set(trim($v.'-table-update-time'),time());
            }
        }
    }
    /**
     * 全局过滤 将所有的 字符关键字过滤掉
     * @param $values
     */
    public function filterScensitive(&$values){
        if (isset($this->model->filter) && $this->model->filter){
            foreach ($values as $k=>$v){
                if (is_string($v) && in_array($k,$this->filter)){
                    // 关键字过滤
                    $values[$k] = MyUtils::checkSensitive($v);
                }
            }
        }
    }
}
